package com.mentaleye.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mentaleye.bean.PromptReadBean;
import com.mentaleye.dal.dao.PromptReadDao;
import com.mentaleye.dal.model.PromptRead;
import com.mentaleye.service.PromptReadService;
import com.mentaleye.util.ObjectUtil;

@Service
public class PromptReadServiceImpl implements PromptReadService {

	@Autowired
	PromptReadDao promptReadDao;

	@Override
	public boolean addPromptRead(Integer promptId, String promptPersonId) {
		PromptRead promptRead = new PromptRead();
		promptRead.setPromptId(promptId);
		promptRead.setPromptPersonId(promptPersonId);
		promptRead.setLastReadTime(new Date());
		promptRead.setReadTimes(1);
		promptRead.setIsNotRead(0);
		boolean boo = promptReadDao.insertPromptRead(promptRead);
		return boo;
	}

	@Override
	public boolean modifyPromptRead(Integer promptId, String promptPersonId, Integer readTimes) {
		PromptRead promptRead = new PromptRead();
		promptRead.setPromptId(promptId);
		promptRead.setPromptPersonId(promptPersonId);
		promptRead.setLastReadTime(new Date());
		promptRead.setReadTimes(readTimes);
		boolean boo = promptReadDao.updatePromptRead(promptRead);
		return boo;
	}

	@Override
	public PromptReadBean queryPromptRead(Integer promptId, String promptPersonId) {
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("promptId", promptId);
		map.put("promptPersonId", promptPersonId);
		PromptReadBean promptReadBean = (PromptReadBean) ObjectUtil
				.transfer(promptReadDao.selectPromptRead(map), PromptReadBean.class);
		return promptReadBean;
	}

	@Override
	public boolean updatePromptRead(List<Integer> ids, String promptPersonId) {
		Map<String, Object> map = new HashMap<String,Object>();
		List<PromptRead> list = new ArrayList<PromptRead>();
		for (Integer integer : ids) {
			PromptRead promptRead = new PromptRead();
			promptRead.setPromptId(integer);
			promptRead.setPromptPersonId(promptPersonId);
			promptRead.setLastReadTime(new Date());
			list.add(promptRead);
		}
		map.put("list", list);
		return promptReadDao.modifyPromptRead(map);
	}

	@Override
	public boolean updatePromptNoRead(List<Integer> ids, String promptPersonId) {
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("list", ids);
		map.put("promptPersonId", promptPersonId);
		return promptReadDao.modifyPromptNoRead(map);
	}

}
